# -*- coding: utf-8 -*-

'''eof
name:申请人征信无历史信贷记录（经营类）
code:IC7_IndNoOprCreditRecord
tableName:
columnName:
groups:个人征信
dependencies:IC_DSJ_2D
type:常用指标
datasourceType:在线指标
description:申请人经营类贷款账户数
eof'''

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

# 获取表信息方法
def getTable(data, tableName, occurTimes, iteratorTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :param iteratorTimes:下级字段出现的次数，如果是0代表下级字段可以不出现，如果是1代表下级字段必须出现
        :return:该表全部数据  返回[{}]为异常缺失，返回0为正常缺失，其他返回正常数组
        '''
    try:
        table = data.get(tableName)
        if occurTimes == 0:
            if table is None or (table == "" and iteratorTimes == 0):
                return 0
            elif isinstance(table, list) is False or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
        if occurTimes == 1:
            if table == "" and iteratorTimes == 0:
                return 0
            if table is None or isinstance(table, list) is False or len(table) == 0 \
                    or (table == "" and iteratorTimes == 1):
                return [{}]
            else:
                return table
    except:
        return [{}]


def IC7_IndNoOprCreditRecord():
    try:
        data = IC_DSJ_2D['data']
        factor_list = []
        # 借贷账户信息
        PDA = getTable(data, 'PDA', 1, 0)
        if PDA == [{}]:
            return u'缺失值'
        elif PDA == 0:
            return 0
        else:
            # 借贷账户信息单元
            PD01 = getTable(PDA[0], 'PD01', 0, 1)
            if PD01 == 0:
                return 0
            elif PD01 == [{}]:
                return u'缺失值'
            else:
                for i in PD01:
                    try:
                        # 基本信息段
                        PD01A = getTable(i, 'PD01A', 1, 1)
                        if PD01A == 0:
                            factor_list.append(0)
                            continue
                        elif PD01A == [{}]:
                            factor_list.append(None)
                        else:
                            # 业务种类
                            PD01AD03 = PD01A[0].get('PD01AD03')
                            if PD01AD03 in null_type_list:
                                factor_list.append(None)
                                continue
                            else:
                                factor_list.append(0)
                            if str(PD01AD03) in ('41', '52'):
                                factor_list.append(1)
                            else:
                                factor_list.append(0)
                    except:
                        factor_list.append(None)
                        continue

        factorNotNone = list(filter(lambda x: x is not None, factor_list))
        factorNotNoneAndZero = list(filter(lambda x: x != 0, factorNotNone))
        if len(factorNotNone) == 0:
            return u'缺失值'
        else:
            return sum(factorNotNoneAndZero)
    except:
        return u'缺失值'


result = IC7_IndNoOprCreditRecord()

